package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.BusinessInfo;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.enums.biz.ActiveTypeEnum;
import com.cq.hd.common.enums.biz.OrderSelectTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.TbBusinessPointsMallApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.*;
import com.cq.hd.order.api.TbOrderApi;
import com.cq.hd.order.api.dto.AppOrderPageDto;
import com.cq.hd.order.api.dto.MyPrizePageDto;
import com.cq.hd.order.api.vo.AppOrderPageVo;
import com.cq.hd.order.api.vo.MyPrizeOrderPageVo;
import com.cq.hd.product.api.TbActiveRedPacketBillApi;
import com.cq.hd.product.api.dto.MyPrizeRedPacketBillPageDto;
import com.cq.hd.product.api.vo.MyPrizeRedPacketBillPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/businessPointsMall")
@Api(tags = "商家积分商城相关接口")
public class BusinessPointsMallController {

    @Autowired
    private TbBusinessPointsMallApi businessPointsMallApi;

    @Autowired
    private TbOrderApi orderApi;

    @Autowired
    private TbActiveRedPacketBillApi redPacketBillApi;

    @ApiOperation(value = "获取积分商城配置")
    @ApiResponse(response = Integer.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/getSettings")
    public Result<BusinessPointsMallSettingsVo> getSettings(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }
        Long businessId = businessInfo.getId();

        return businessPointsMallApi.getSettings(businessId);
    }

    @ApiOperation(value = "设置积分商城配置")
    @ApiImplicitParam(name = "businessPointsMallSettingsDto", value = "实体", required = true, paramType = "body", dataType = "BusinessPointsMallSettingsDto")
    @PostMapping("/saveSettings")
    public Result<Boolean> saveSettings(@ApiIgnore @LoginUser AppLoginUser appLoginUser, @RequestBody BusinessPointsMallSettingsDto businessPointsMallSettingsDto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }
        businessPointsMallSettingsDto.setBusinessId(businessInfo.getId());

        return businessPointsMallApi.saveSettings(businessPointsMallSettingsDto);
    }

    @ApiOperation(value = "优惠券/核销商品列表[商家自己查看]")
    @ApiResponse(response = Integer.class, code = 200, message = "接口返回对象参数")
    @ApiImplicitParam(name = "productListDto", value = "实体", paramType = "body", dataType = "BusinessPointsMallProductListDto")
    @GetMapping("/myProductList")
    public Result<List<BusinessPointsMallProductVo>> myProductList(@ApiIgnore @LoginUser AppLoginUser appLoginUser, BusinessPointsMallProductListDto productListDto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }
        productListDto.setBusinessId(businessInfo.getId());
        productListDto.setViewType(1);

        return businessPointsMallApi.getProductList(productListDto);
    }

    @ApiOperation(value = "优惠券/核销商品列表[别人查看]")
    @ApiResponse(response = Integer.class, code = 200, message = "接口返回对象参数")
    @ApiImplicitParam(name = "productListDto", value = "实体", paramType = "body", dataType = "BusinessPointsMallProductListDto")
    @GetMapping("/getProductList")
    public Result<List<BusinessPointsMallProductVo>> getProductList(BusinessPointsMallProductListDto productListDto) {
        Long businessId = productListDto.getBusinessId();
        if (businessId == null || businessId == 0) {
            Throw.isBusinessException("请选择要查看的商家");
        }
        productListDto.setBusinessId(businessId);
        productListDto.setViewType(2);

        return businessPointsMallApi.getProductList(productListDto);
    }

    @ApiOperation(value = "添加编辑优惠券")
    @ApiImplicitParam(name = "businessPointsMallCouponDto", value = "实体", required = true, paramType = "body", dataType = "BusinessPointsMallCouponDto")
    @PostMapping("/saveCoupon")
    public Result<Boolean> saveCoupon(@ApiIgnore @LoginUser AppLoginUser appLoginUser, @RequestBody BusinessPointsMallCouponDto businessPointsMallProductDto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }
        businessPointsMallProductDto.setBusinessId(businessInfo.getId());

        return businessPointsMallApi.saveCoupon(businessPointsMallProductDto);
    }

    @ApiOperation(value = "添加编辑核销商品")
    @ApiImplicitParam(name = "businessPointsMallGoodsDto", value = "实体", required = true, paramType = "body", dataType = "BusinessPointsMallGoodsDto")
    @PostMapping("/saveGoods")
    public Result<Boolean> saveGoods(@ApiIgnore @LoginUser AppLoginUser appLoginUser, @RequestBody BusinessPointsMallGoodsDto businessPointsMallGoodsDto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }
        businessPointsMallGoodsDto.setBusinessId(businessInfo.getId());

        return businessPointsMallApi.saveGoods(businessPointsMallGoodsDto);
    }

    @ApiOperation(value = "删除优惠券、核销商品")
    @ApiImplicitParam(name = "dto", value = "id", required = true, paramType = "body", dataType = "PointsMallProductDto")
    @PostMapping("/removeProduct")
    public Result<Boolean> removeProduct(@ApiIgnore @LoginUser AppLoginUser appLoginUser,  @RequestBody PointsMallProductDto dto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        return businessPointsMallApi.removeProduct(dto.getId(), businessInfo.getId());
    }

    @ApiOperation(value = "开启积分商城")
    @PostMapping("/open")
    public Result<Boolean> open(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        return businessPointsMallApi.open(businessInfo.getId());
    }

    @ApiOperation(value = "关闭积分商城")
    @PostMapping("/close")
    public Result<Boolean> close(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        return businessPointsMallApi.close(businessInfo.getId());
    }

    @ApiOperation(value = "策划人调整积分")
    @PostMapping("/businessChangePoints")
    @ApiImplicitParam(name = "businessChangePointsDto", value = "实体", required = true, paramType = "body", dataType = "BusinessChangePointsDto")
    public Result<Boolean> businessChangePoints(@ApiIgnore @LoginUser AppLoginUser appLoginUser, @RequestBody BusinessChangePointsDto businessChangePointsDto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }
        businessChangePointsDto.setBusinessId(businessInfo.getId());

        return businessPointsMallApi.businessChangePoints(businessChangePointsDto);
    }

    @ApiOperation(value = "用户积分列表")
    @ApiResponse(response = UserPointsAccountPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageUserPointsAccount")
    public Result<Page<UserPointsAccountPageVo>> pageUserPointsAccount(@ApiIgnore @LoginUser AppLoginUser appLoginUser, UserPointsAccountPageDto userPointsAccountPageDto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }
        userPointsAccountPageDto.setBusinessId(businessInfo.getId());

        return businessPointsMallApi.pageUserPointsAccount(userPointsAccountPageDto);
    }

    @ApiOperation(value = "用户积分详情")
    @ApiImplicitParam(name = "activeId", value = "用户id", paramType = "query", dataType = "Long")
    @ApiResponse(response = UserPointsAccountPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/userPointsAccount")
    public Result<UserPointsAccountPageVo> userPointsAccount(@ApiIgnore @LoginUser AppLoginUser appLoginUser, UserPointsDetailPageDto userPointsDetailPageDto) {
        // 如果有传商家id，表示在商家主页查看自己的明细，李总又胡乱加需求
        Long businessId = userPointsDetailPageDto.getBusinessId();
        if (businessId != null && businessId > 0) {
            Long userId = appLoginUser.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException("请先登录");
            }
            userPointsDetailPageDto.setUserId(userId);
            userPointsDetailPageDto.setBusinessId(businessId);
        } else {
            // 没有传商家id，表示商家查看自己底下的用户的明细
            BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
            if (businessInfo == null || businessInfo.getId() == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }
            userPointsDetailPageDto.setBusinessId(businessInfo.getId());
        }

        return businessPointsMallApi.userPointsAccount(userPointsDetailPageDto.getBusinessId(), userPointsDetailPageDto.getUserId());
    }

    @ApiOperation(value = "用户积分明细")
    @ApiResponse(response = UserPointsDetailPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageUserPointsDetail")
    public Result<Page<UserPointsDetailPageVo>> pageUserPointsDetail(@ApiIgnore @LoginUser AppLoginUser appLoginUser, UserPointsDetailPageDto userPointsDetailPageDto) {
        // 如果有传商家id，表示在商家主页查看自己的明细，李总又胡乱加需求
        Long businessId = userPointsDetailPageDto.getBusinessId();
        if (businessId != null && businessId > 0) {
            Long userId = appLoginUser.getUserId();
            if (userId == null || userId <= 0) {
                Throw.isBusinessException("请先登录");
            }
            userPointsDetailPageDto.setUserId(userId);
            userPointsDetailPageDto.setBusinessId(businessId);
        } else {
            // 没有传商家id，表示商家查看自己底下的用户的明细
            BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
            if (businessInfo == null || businessInfo.getId() == null) {
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }
            userPointsDetailPageDto.setBusinessId(businessInfo.getId());
        }

        return businessPointsMallApi.pageUserPointsDetail(userPointsDetailPageDto);
    }

    @ApiOperation(value = "商家积分商城主页")
    @ApiImplicitParam(name = "id", value = "商家id", required = true, paramType = "query", dataType = "Long")
    @ApiResponse(response = BusinessPointsMallHomePageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/getHomePage")
    public Result<BusinessPointsMallHomePageVo> getHomePage(@ApiIgnore @LoginUser AppLoginUser appLoginUser, Long id) {
        if (id == null || id <= 0) {
            Throw.isBusinessException("请选择查看的商家");
        }

        return businessPointsMallApi.getHomePage(id, appLoginUser.getUserId());
    }

    @ApiOperation(value = "积分商品详情")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "body", dataType = "Long")
    @GetMapping("/productDetail")
    public Result<BusinessPointsMallProductDetailVo> productDetail(@ApiIgnore @LoginUser AppLoginUser appLoginUser, Long id) {
        Long userId = appLoginUser.getUserId();
        if (userId == null || userId <= 0) {
            Throw.isBusinessException("请先登录");
        }
        return businessPointsMallApi.productDetail(id, userId);
    }

    @ApiOperation(value = "积分兑换")
    @ApiImplicitParam(name = "dto", value = "id", required = true, paramType = "body", dataType = "PointsMallProductDto")
    @PostMapping("/exchange")
    public Result<Boolean> exchange(@ApiIgnore @LoginUser AppLoginUser appLoginUser, @RequestBody PointsMallProductDto dto) {
        return businessPointsMallApi.exchange(dto.getId(), appLoginUser.getUserId());
    }

    @ApiOperation(value = "积分订单列表")
    @ApiResponse(response = UserPointsAccountPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pagePointsOrder")
    public Result<Page<AppOrderPageVo>> pagePointsOrder(@ApiIgnore @LoginUser AppLoginUser appLoginUser, AppOrderPageDto appOrderPageDto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }
        appOrderPageDto.setBusinessId(businessInfo.getId());
        appOrderPageDto.setOrderSelectType(OrderSelectTypeEnum.SELL.getValue());
        appOrderPageDto.setOrderTypes(Collections.singletonList(ActiveTypeEnum.POINTS.getValue()));
        return orderApi.pageAppOrder(appOrderPageDto);
    }

    @ApiOperation(value = "我的奖品[卡券]")
    @ApiResponse(response = UserPointsAccountPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageMyPrizeRedPacket")
    public Result<Page<MyPrizeRedPacketBillPageVo>> pageMyPrizeRedPacket(@ApiIgnore @LoginUser AppLoginUser appLoginUser, MyPrizeRedPacketBillPageDto myPrizeRedPacketBillPageDto) {
        Long userId = appLoginUser.getUserId();
        if (userId == null || userId <= 0) {
            Throw.isBusinessException("请先登录");
        }
        Long businessId = myPrizeRedPacketBillPageDto.getBusinessId();
        if(businessId == null || businessId <= 0) {
            Throw.isBusinessException("请选择商家");
        }
        myPrizeRedPacketBillPageDto.setUserId(userId);
        return redPacketBillApi.pageMyPrizeRedPacket(myPrizeRedPacketBillPageDto);
    }

    @ApiOperation(value = "我的奖品[商品]")
    @ApiResponse(response = UserPointsAccountPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageMyPrizeOrder")
    public Result<Page<MyPrizeOrderPageVo>> pageMyPrizeOrder(@ApiIgnore @LoginUser AppLoginUser appLoginUser, MyPrizePageDto myPrizePageDto) {
        Long userId = appLoginUser.getUserId();
        if (userId == null || userId <= 0) {
            Throw.isBusinessException("请先登录");
        }
        Long businessId = myPrizePageDto.getBusinessId();
        if(businessId == null || businessId <= 0) {
            Throw.isBusinessException("请选择商家");
        }
        myPrizePageDto.setUserId(userId);
        return orderApi.pageMyPrizeOrder(myPrizePageDto);
    }

    @ApiOperation(value = "积分商城分享")
    @ApiImplicitParam(name = "businessId", value = "商家id", paramType = "query", dataType = "Long")
    @ApiResponse(response = BusinessPointsMallShareVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/share")
    public Result<BusinessPointsMallShareVo> share(Long businessId) {
        if (businessId == null || businessId <= 0) {
            Throw.isBusinessException("商家id不能为空");
        }
        return businessPointsMallApi.share(businessId);
    }
}
